package com.vc.videosdk.utils

import android.app.Activity
import android.text.Editable
import android.text.TextUtils
import android.text.TextWatcher
import android.util.Log
import android.view.KeyEvent
import android.view.View
import android.view.inputmethod.EditorInfo
import android.widget.EditText
import com.vc.videosdk.xlog.XLogUtil

/**

 * @Author : wy

 * @Time : On 2024/9/27 14:36

 * @Description : SteamKeyboardUtils

 */
class SteamKeyboardUtil {
    private var mEdtKeybroad: EditText? = null
    private var isAutoEdit = false
    private var mActivity: Activity? = null
    companion object {
        private var singleInstance: SteamKeyboardUtil? = null
            get() {
                // 懒汉模式
                if (null == field) {
                    field = SteamKeyboardUtil()
                }
                return field
            }

        @Synchronized // 添加注解，线程同步，线程安全
        fun getInstance(): SteamKeyboardUtil {
            return singleInstance!! // 表示非空时执行
        }
    }



    fun initEditText(edt: EditText) {
        mEdtKeybroad = edt
        edt.setOnKeyListener(object : View.OnKeyListener {
            override fun onKey(p0: View?, keyCode: Int, keyEvent: KeyEvent?): Boolean {
                if (keyEvent == null) {
                    return false
                }
                XLogUtil.d("SteamKeyboardUtils setOnKeyListener KeyEvent:$keyEvent")
                //删除键
                if (keyCode == KeyEvent.KEYCODE_DEL && keyEvent.action == KeyEvent.ACTION_DOWN) {
                    XLogUtil.d("SteamKeyboardUtils KeyEvent.KEYCODE_DEL")
                    //TODO 调用删除
//                    val jsonData: String = keyBoardInfo("", -1).toString()
//                    instructionsSocket?.sendSlideData(jsonData)
                    return true
                } else if (keyCode == KeyEvent.KEYCODE_ENTER && keyEvent.action == KeyEvent.ACTION_DOWN) {
                    XLogUtil.d("SteamKeyboardUtils KeyEvent.KEYCODE_ENTER")
                    //回车键
                    editFinish()
                    return true
                }
                return false
            }
        })

        edt.setOnEditorActionListener { textView, actionId, keyEvent ->
            XLogUtil.d("SteamKeyboardUtils actionId:$actionId, $keyEvent")
            //回车键
            if (actionId == EditorInfo.IME_ACTION_DONE) {
                editFinish()
                true;
            }
            false
        }
        edt.removeTextChangedListener(mTextWatcher)
        edt.addTextChangedListener(mTextWatcher)
    }


    /**
     * 编辑结束键盘收起
     */
    fun editFinish() {
        //TODO 发送键盘结束
        //instructionsSocket?.sendKeyCode("66")
    }

    private val mTextWatcher: TextWatcher = object : TextWatcher {
        override fun beforeTextChanged(charSequence: CharSequence, i: Int, i1: Int, i2: Int) {}
        override fun onTextChanged(charSequence: CharSequence, start: Int, before: Int,
                                   count: Int) {
            //Log.d("AAAAA","editTextData"+charSequence)
            if (isAutoEdit) {
                isAutoEdit = false
                return
            }
            try {
                if (!TextUtils.isEmpty(charSequence)) {
                    XLogUtil.d("SteamKeyboardUtils onTextChanged editTextData:${charSequence}, start:$start,before$before:,count:$count")
                    if ((charSequence.length > (start + before))) {
                        //走新增  TODO
                        val editTextData = charSequence.toString().substring(start + before)
                        Log.d("AAAAA","editTextData:"+editTextData)
                    //                        val jsonData: String = keyBoardInfo(editTextData, 1).toString()
//                        instructionsSocket?.sendSlideData(jsonData)
                    }else if(charSequence.length < (start + before)){
                        //走删除 TODO
//                        val jsonData: String = keyBoardInfo("", -1).toString()
//                        instructionsSocket?.sendSlideData(jsonData)
                    }
                }
            }catch (e:Exception){
                e.printStackTrace()
            }
        }

        override fun afterTextChanged(editable: Editable) {}
    }




    /**
     * 调起键盘
     */
    fun showKeyboard( activity: Activity) {
        isAutoEdit = true

        mEdtKeybroad?.let {
            XLogUtil.d("SteamKeyboardUtil showKeyBoard")
            try {
                KeyboardUtil.showSoftInput(it)
            }catch (e:Exception){
                Log.d("AAAAA","showSoftInput Exception:"+e.message)
            }


           // KeyboardUtils.registerSoftInputChangedListener(mActivity!!,onSoftInputChangedListener)
        }
    }




    /**
     * 隐藏键盘
     * @param isUserTouch 是否socket通知关闭键盘
     */
    fun hideKeyBoard(isSocketMessage:Boolean) {
        XLogUtil.d("SteamKeyboardUtil hideKeyBoard")
//        if(softKeyboardStateHelper?.isSoftKeyboardOpened == false){
//            return
//        }

        mEdtKeybroad?.let {
            KeyboardUtil.hideSoftInput(it)
        }
    }


}